home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / moustrk.com / MOUSTRAK.C < prev   
Encoding:
C/C++ Source or Header  |  1989-09-18  |  4.1 KB  |  152 lines

  1. /*------------------------------------------------------------
  2.  moustrak.c  -- An example of using a mouse event handler.
  3.  Bob Goldrich  8/31/89
  4. ------------------------------------------------------------*/
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <dos.h>
  8.  
  9. #define  MOUSEINT  geninterrupt( 0x33 )
  10.  
  11. /*-- mouse event masks (use with M_event) --*/
  12. #define  MOUSEMOVE     0x01        /* Use, for example:     */
  13. #define  L_PRESS        0x02       /* L_PRESS | MOUSEMOVE  */
  14. #define  L_RELEASE      0x04
  15. #define  R_PRESS        0x08
  16. #define  R_RELEASE      0x10
  17.  
  18. /*-- mouse button status macros --*/
  19. #define  L_DOWN         ( M_buttonstatus & 0x01 )
  20. #define  R_DOWN         ( M_buttonstatus & 0x02 )
  21.  
  22. #define  HANDLER_EXIT_PROCESSING()  \
  23.  __emit__( (unsigned char) 0x5D,  \
  24.            (unsigned char) 0x5F,  \
  25.            (unsigned char) 0x5E,  \
  26.            (unsigned char) 0x1F,  \
  27.            (unsigned char) 0x07,  \
  28.            (unsigned char) 0x5A,  \
  29.            (unsigned char) 0x59,  \
  30.            (unsigned char) 0x5B,  \
  31.            (unsigned char) 0x58,  \
  32.            (unsigned char) 0xCB  ) ;
  33. #if 0
  34. -------------------------------------------
  35. The above emmited code is equivalent to the
  36. following assembler code (but does not
  37. require the -B compiling option):
  38.        asm  pop   bp    ;
  39.        asm  pop   di    ;
  40.        asm  pop   si    ;
  41.        asm  pop   ds    ;
  42.        asm  pop   es    ;     /* interrupt exit processing */
  43.        asm  pop   dx    ;
  44.        asm  pop   cx    ;
  45.        asm  pop   bx    ;
  46.        asm  pop   ax    ;
  47.        asm  retf        ;     /* far return */
  48. --------------------------------------------
  49. #endif
  50.  
  51. /*--Global mouse status variables --*/
  52. int  M_xpos, M_ypos,           /* cursor location */
  53.      M_buttonstatus,           /* bits 0-2 ON if button is down */
  54.      M_eventcount,             /* # of times event has occured */
  55.      M_event ;                 /* flags a mouse event */
  56.  
  57. /*-- Reset mouse -- returns # of buttons or 0 if problems --*/
  58. int Mreset( void )
  59. {
  60.  _AX = 0 ;
  61.  MOUSEINT ;
  62.  return( _AX  ?  _BX  : _AX ) ;
  63. }
  64.  
  65. /*-- Show mouse cursor --*/
  66. void Mshow( void )
  67. {
  68.  _AX = 1 ;
  69.  MOUSEINT ;
  70. }
  71.  
  72. /*-- Hide mouse cursor --*/
  73. void Mhide( void )
  74. {
  75.  _AX = 2 ;
  76.  MOUSEINT ;
  77. }
  78.  
  79. /*-- Get mouse position and button status --*/
  80. void Mpos( void )
  81. {
  82.  _AX = 3 ;
  83.  MOUSEINT ;
  84.  M_xpos = _CX ;
  85.  M_ypos = _DX ;
  86.  M_buttonstatus = _BX ;
  87. }
  88.  
  89. /*-- Installs mouse handler --*/
  90. void Minsthandler( unsigned mask, void interrupt (*fn)(void) )
  91. {
  92.  union REGS   reg ;
  93.  struct SREGS seg ;
  94.  reg.x.ax = 20 ;      /* mouse fn. 20 -- replaces fn.12 */
  95.  reg.x.cx = mask ;
  96.  reg.x.dx = FP_OFF( fn ) ;
  97.  seg.es = FP_SEG( fn ) ;
  98.  int86x( 0x33, ®, ®, &seg ) ;
  99. }
  100.  
  101. /*------------------------------------------------------------
  102.  This mouse event handler simply updates mouse information.
  103. ------------------------------------------------------------*/
  104. void interrupt mousehandler( void )
  105. {
  106.  M_event = _AX ;
  107.  M_buttonstatus = _BX ;
  108.  M_xpos = _CX ;
  109.  M_ypos = _DX ;
  110.  HANDLER_EXIT_PROCESSING() ;
  111. }
  112.  
  113. /*----------------------------------------------------
  114.  Demonstrate mouse tracking via mouse event handler.
  115. ----------------------------------------------------*/
  116. main()
  117. {
  118.  clrscr() ;
  119.  if( !Mreset() ) {
  120.     fputs("Cannot initialize mouse.", stderr) ;
  121.     exit(1) ;
  122.  }
  123.  
  124.  Minsthandler( MOUSEMOVE, mousehandler ) ;
  125.  M_event = 0 ;
  126.  Mshow() ;
  127.  
  128.  gotoxy(20,24) ;
  129.  cprintf("Text mode mouse tracking with event handler") ;
  130.  gotoxy(20,25) ;
  131.  cprintf("        Press any KEY to quit.") ;
  132.  gotoxy( 1, 1) ;
  133.  cprintf("Position and button status is updated only when the mouse moves.") ;
  134.  gotoxy( 1, 4);
  135.  cprintf("Button Status at time of handler call:") ;
  136.  
  137.  while( !kbhit() ) {
  138.     if( M_event ) {
  139.        gotoxy(1,2) ;
  140.        cprintf("Cursor position: %3d, %3d", M_xpos/8+1, M_ypos/8+1 ) ;
  141.        gotoxy(1,5) ;
  142.        L_DOWN ? cprintf("Left  DOWN") : cprintf("Left  UP  ") ;
  143.        gotoxy(20,5) ;
  144.        R_DOWN ? cprintf("Right DOWN") : cprintf("Right UP  ") ;
  145.        M_event = 0 ;      /* reset the event */
  146.     }
  147.  }
  148.  Mhide() ;
  149.  Mreset() ;
  150. }
  151.  
  152.